//建议
var weiAccountQqGroup = {
    //请求url
    listUrl: "/weiAccountQqGroup/list.action?accountId=", //列表地址
    overviewListUrl: "/weiAccountQqGroup/overviewList.action", //QQ群分配概览列表地址
    toAddUrl: "/weiAccountQqGroup/toAssign.action?groupIds=", //去添加页面地址
    addUrl: "/weiAccountQqGroup/add.action", //添加地址
    toMembersUrl: "/tgQqMember/list.action?qqGroup=", //去修改页面地址
    editUrl: "/weiAccountQqGroup/edit.action", //去修改页面地址
    deleteUrl: "/weiAccountQqGroup/delete.action?id=", //删除页面地址
    toViewUrl: "/weiAccountQqGroup/toView.action?id=", //详细页面地址
    getByIdUrl: "/weiAccountQqGroup/getById.action", //根据id查询对象
    modifyDeleteStatusUrl: "/weiAccountQqGroup/modifyDeleteStatus.action", //停用 或启用
    checkNameExitsUrl: "/weiAccountQqGroup/checkNameExits.action", //检验唯一性,
    helpUrl: "/weiAccountQqGroup/toHelp.action",
    uploadJlUrl: "/zpJlInfo/toAdd.action?sourceType=1&accountType=8&qqGroupId=",//上传群内简历
    complainLeaderUrl: "/weiAccountQqGroupLeader/complainLeader.action",//投诉群主
    recommendFlowListUrl: "/zpRecommendFlowInfo/weiList.action?qqGroupNumber=",//推荐流程列表

    onlyName: $("#onlyName"), //修改，唯一验证时需要添加此属性
    m_title: $(".m_title"),//页面标题
    zeroLevel: $("#zeroLevel"),//页面一级导航位置
    firstLevel: $("#firstLevel"),//页面一级导航位置
    pageTitle: $("title"),//页面标题
    _overview_title_val: "QQ群分配",
    _detail_title_val: "QQ群分配明细",
    _assign_title_val: "QQ群分配用户",
    _title_val: "QQ群",

    addform: jQuery("#addform"), //添加form
    queryfrom: jQuery("#queryForm"), //查询form
    changeform: jQuery("#changeform"),
    addBtn: $("#addBtn"),//添加按钮
    queryBtn: $("#queryBtn"),//查询按钮
    group_left_menu_selected_id: "tgQqGroup_list",
    overview_left_menu_selected_id: "weiAccountQqGroupOverview_list",   //左侧菜单选择id
    left_menu_selected_id: "weiAccountQqGroup_list",   //左侧菜单选择id


    //属性
    id: $("#id"), //id
    username: $("#username"), //姓名
    groupNumber: $("#groupNumber"), //群号
    categoryName: $("#categoryName"), //群分类
    groupName: $("#groupName"), //群名称
    inPersonName: $("#inPersonName"), //分配人
    inTime: $("#inTime"), //录入时间（分配的时间）
    addStatusName: $("#addStatusName"), //添加状态，默认待添加
    addStatusTime: $("#addStatusTime"), //已通过的时间，或未通过的时间，默认为null
    addNoPassReason: $("#addNoPassReason"), //添加群未通过的原因
    importStatusName: $("#importStatusName"), //导入状态，默认未导入
    importTime: $("#importTime"), //导入成员时间，默认为null
    kickStatusName: $("#kickStatusName"), //是否被踢，默认未被踢
    kickTime: $("#kickTime"), //被踢时间
    kickReason: $("#kickReason"), //被踢的原因
    qqMemberCount: $("#qqMemberCount"), //qq成员数
    updateTime: $("#updateTime"), //更新时间
    groupIds: $("#groupIds"),
    //页面初始化
    initPage: function () {
        var self = this;
        self.currPage = common.getCurrPageFlag();
        common.initLeftMenuSelected(self.left_menu_selected_id);

        //初始化标题
        common.initPageTitle(self._title_val, self.m_title);
        common.initTitle(self._title_val, self.pageTitle, true);
        common.initTitle(self._title_val, self.zeroLevel, false);
        common.initTitle(self._title_val, self.firstLevel, false);
        //列表页面
        if (self.currPage != "list") {
            self.addform.validationEngine({scroll: false});
            self.addBtn.unbind("click").click(function () {
                self.add();
            });
        }
        if (self.currPage == "list") {
            self.initSeletePage();
        } else if (self.currPage == "edit") {
            self.initEditPage();
        } else if (self.currPage == "add") {
            self.initAddPage();
        } else if (self.currPage == "detail") {
            self.initDetailPage();
        }
    },
    initAssignPage: function () {
        var self = this;
        self.currPage = common.getCurrPageFlag();
        common.initLeftMenuSelected(self.group_left_menu_selected_id);
        //初始化标题
        common.initPageTitle(self._assign_title_val, self.m_title);
        common.initTitle(self._title_val, self.pageTitle, true);
        common.initTitle(self._title_val, self.zeroLevel, false);
        common.initTitle(self._title_val, self.firstLevel, false);
        self.queryfrom.validationEngine({scroll: false});
        self.queryBtn.unbind("click").click(function () {
            self.queryAccount();
        });
        self.initAddPage();
    },
    initSeletePage: function () {
        var self = this;
        self.queryfrom.validationEngine({scroll: false});
        self.queryBtn.unbind("click").click(function () {
            self.query();
        });
    },
    initOverviewPage: function () {
        var self = this;
        self.currPage = common.getCurrPageFlag();
        common.initLeftMenuSelected(self.overview_left_menu_selected_id);
        //初始化标题
        common.initPageTitle(self._overview_title_val, self.m_title);
        common.initTitle(self._overview_title_val, self.pageTitle, true);
        common.initTitle(self._title_val, self.zeroLevel, false);
        common.initTitle(self._overview_title_val, self.firstTitle, false);
        self.queryfrom.validationEngine({scroll: false});
        self.queryBtn.unbind("click").click(function () {
            self.queryOverview();
        });
    },
    //列表查询
    query: function () {
        var self = this;
        var b = self.queryfrom.validationEngine('validate');
        if (!b) {
            return false;
        }
        var serializeObj = common.serializeJson("queryForm");
        var jsonStr = JSON.stringify(serializeObj)
        location.href = this.listUrl + "&jsonParam=" + jsonStr;
    },
    //列表查询
    queryOverview: function () {
        var self = this;
        var b = self.queryfrom.validationEngine('validate');
        if (!b) {
            return false;
        }
        var serializeObj = common.serializeJson("queryForm");
        var jsonStr = JSON.stringify(serializeObj)
        location.href = this.overviewListUrl + "?jsonParam=" + jsonStr;
    },
    //用户列表查询
    queryAccount: function () {
        var self = this;
        var b = self.queryfrom.validationEngine('validate');
        if (!b) {
            return false;
        }
        var serializeObj = common.serializeJson("queryForm");
        var jsonStr = JSON.stringify(serializeObj)
        location.href = this.toAddUrl + self.groupIds.val() + "&jsonParam=" + jsonStr;
    },
    //跳转列表页面
    tolist: function (id) {
        location.href = this.listUrl + id;
    },
    //跳转到添加页面
    toAdd: function () {
        location.href = this.toAddUrl + this.groupIds.val();
    },
    //跳转修改页面
    toMembers: function (id) {
        location.href = this.toMembersUrl + id;
    },
    //跳转详情页面
    toDetail: function (id) {
        location.href = this.toViewUrl + id;
    },
    //跳转到上传群内简历页面
    toUploadJl: function (id) {
        location.href = this.uploadJlUrl + id;
    },
    toRecommendFlow : function(groupNumber){
        location.href = this.recommendFlowListUrl + groupNumber;
    },
    //初始化添加页面
    initAddPage: function () {
        var self = this;
        self.addform.attr("action", self.addUrl);
        //检验名称唯一性
        /*self.name.blur(function(){
         if($.trim(this.value).length>0){
         var param = {flag : common.getCurrPageFlag(),name:this.value};
         self.checkNameExits(param,self.checkNameExitCallBack);
         }
         });*/


    },
    //初始化详情页面数据
    initDetailPage: function () {
        var self = this;
        self.getById(self.id.val(), function (result) {
            if (result.s > 0) {
                self.setForm(result.data);
                $("input").attr("disabled", true);
                $("textarea").attr("disabled", true);
                self.addBtn.hide();
                $("._detail").show();
            }//不存在
            else if (result.s == -1000) {
                location.href = common.notFindUrl;
            }
            else {
                common.alert(result.d);
            }
        });
    },

    //建议 添加
    add: function () {
        var self = this;
        var selected = $("input:radio[name='selectItem']:checked");
        if (selected.length == 0) {
            alert("请选择要分配的用户！");
            return;
        }
        $("#accountId").val(selected.val());
        self.ajaxSubmitForm();
    },
    //提交表单
    ajaxSubmitForm: function () {
        var self = this;
        self.addform.ajaxSubmit(function (resp) {
            if (resp.s > 0) {
                location.href = self.overviewListUrl;
            } else {
                //唯一判断
                if (resp.s == -100) {
                    $("#username").validationEngine("showPrompt", resp.d, "error");
                }
            }
        });
    },
    //初始化修改页面数据
    initEditPage: function () {
        var self = this;
        self.getById(this.id.val(), function (result) {
            if (result.s > 0) {
                self.setForm(result.data);
            }//不存在
            else if (result.s == -1000) {
                location.href = common.notFindUrl;
            }
            else {
                common.alert(resp.d);
            }
        });
        self.addform.attr("action", self.editUrl);
        //检验名称唯一性
        /*self.name.blur(function(){
         if($.trim(this.value).length>0){
         var param = {flag : common.getCurrPageFlag(),name:this.value,oldname:self.onlyName.val()};
         self.checkNameExits(param,self.checkNameExitCallBack);
         }
         });*/
    },
    //根据id查询信息
    getById: function (id, callBack) {
        var self = this;
        var obj = null;
        var rData = {"id": id};
        $.ajax({
            url: self.getByIdUrl,
            data: rData,//
            success: callBack
        });
    },
    //删除
    toDelete: function (id) {
        var self = this;
        common.openModal("delete_sure", "确定删除吗？");
        $("#delete_sure_a").unbind("click").click(function () {
            location.href = self.deleteUrl + id;
        });
    },
    //更新删除状态
    modifyDeleteStatus: function (id, status) {
        var self = this;
        var delTitle = "";
        if (status == 1) {
            status = 2;
            delTitle = "确定暂停吗?";
        } else if (status == 2) {
            status = 1;
            delTitle = "确定启用吗？";
        }
        common.openModal("delete_sure", delTitle);
        $("#delete_sure_a").unbind("click").click(function () {
            location.href = self.modifyDeleteStatusUrl + "?id=" + id + "&deleteStatus=" + status;
        });
    },
    //填充表单数据
    setForm: function (obj) {
        var self = this;
        //唯一验证时需要记录原来的名称
        self.onlyName.val(obj.name);
        //赋值
        self.id.val(obj.id); //id
        self.username.val(obj.username); //姓名
        self.groupNumber.val(obj.groupNumber); //群号
        self.categoryName.val(obj.categoryName); //群分类
        self.groupName.val(obj.groupName); //群名称
        self.addStatusName.val(obj.addStatusName); //添加状态，默认待添加
        if (obj.addStatusTime && obj.addStatusTime > 0) {
            var new1 = new Date(obj.addStatusTime).format("yyyy-MM-dd");
            self.addStatusTime.val(new1); //已通过的时间，或未通过的时间，默认为null
        }
        self.addNoPassReason.html(obj.addNoPassReason); //添加群未通过的原因
        self.importStatusName.val(obj.importStatusName); //导入状态
        if (obj.importTime && obj.importTime > 0) {
            var new1 = new Date(obj.importTime).format("yyyy-MM-dd");
            self.importTime.val(new1); //导入成员时间，默认为null
        }
        self.kickStatusName.val(obj.kickStatusName); //被踢状态
        if (obj.kickTime && obj.kickTime > 0) {
            var new1 = new Date(obj.kickTime).format("yyyy-MM-dd");
            self.kickTime.val(new1); //被踢时间
        }
        self.kickReason.html(obj.kickReason); //被踢的原因
        self.qqMemberCount.val(obj.qqMemberCount); //qq成员数
        self.inPersonName.val(obj.inPersonName);

        //其它属性
        if (obj.inTime && obj.inTime > 0) {
            var new1 = new Date(obj.inTime).format("yyyy-MM-dd");
            self.inTime.val(new1); //录入时间（分配的时间）
        }

        //其它属性
        if (obj.updateTime && obj.updateTime > 0) {
            var new1 = new Date(obj.updateTime).format("yyyy-MM-dd");
            self.updateTime.val(new1); //更新时间
        }
    },
    //检查名称唯一性
    checkNameExits: function (param, callBack) {
        var self = this;
        $.ajax({
            url: self.checkNameExitsUrl,
            data: param,//
            success: callBack
        });
    },
    //检查名称，回调
    checkNameExitCallBack: function (result) {
        if (result.s < 0) {
            common.alert(result.d, 2000);
        }
    },
    //投诉群主
    complainLeader: function (qqGroupNumber) {
        var self = this;
        $("#complain_sure").modal().css({
            'margin-top': function () {
                return -($(this).height() / 2)
            },
            'margin-left': function () {
                return -($(this).width() / 2);
            }
        });

        if(self.editor){
            self.editor.remove();
        }

        self.editor = KindEditor.create('textarea[name="complainEndFeedback"]', {
            //allowPreviewEmoticons : false,
            uploadJson: '/file/kindEditorUplad.action', //上传
            fileManagerJson: 'file_manager.do', // 文件管理
            allowFileManager: true,
            items: [
                'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                'insertunorderedlist', '|', 'emoticons', 'image', 'link']
        });
        $("#complainEnd_a").unbind("click").click(function () {
            if (self.editor.isEmpty()) {
                common.alert("投诉内容不能为空!");
                return;
            }
            $.post(
                self.complainLeaderUrl,
                {
                    content: self.editor.html(),
                    qqGroupNumber: qqGroupNumber
                },
                function (result) {
                    if (result.s == 1) {
                        location.reload();
                    } else {
                        common.alert(result.d);
                    }
                }
            );
        });
    },
//导入群成员
    toImport: function (id, groupId) {
        $("#importId").val(id);
        $('#importResult').html('');
        $('#importDetail').html('');
        var self = this;
        $("#importMembersPop").modal().css({
            'width': '500px',
            'margin-top': '-150px',
            'margin-left': function () {
                return -($(this).width() / 2);
            }
        });
        $("#importBtn").unbind("click").click(function () {
            self.importMembers(id);
        });
        $("#finishBtn").unbind("click").click(function () {
            self.toMembers(groupId);
        });
        $("#help").unbind("click").click(function () {
            window.open(self.helpUrl);
        });
    }
    ,
    importMembers: function (id) {
        sender = $('#ui-upload-input').get(0);
        if (!sender.value.endWith("txt")) {
            hiOverAlert("目前只支持上传txt文件", 1000);
            return;
        }
        var self = this;
        $('#importform').ajaxSubmit(function (resp) {
            if (resp.s == 1) {
                $('#importResult').html("<span class='green'>导入成功！</span>");
                $('#importDetail').html("<span class='green'>" + resp.d + "</span>");
            } else {
                $('#importResult').html("<span class='red'>导入失败！请联系管理员！</span>");
                // $('#importDetail').html("<span class='red'>"+resp.d+"</span>");
            }
        });
    }
}
